package io.cici.cc.mybatis.plus.core.injector.methods;

import io.cici.cc.mybatis.lite.mapping.MappedStatement;
import io.cici.cc.mybatis.lite.mapping.SqlSource;
import io.cici.cc.mybatis.plus.core.enums.SqlMethod;
import io.cici.cc.mybatis.plus.core.injector.Method;
import io.cici.cc.mybatis.plus.core.metadata.TableInfo;
import io.cici.cc.mybatis.plus.core.toolkit.sql.SqlScriptUtils;

/**
 * 根据 ID 集合删除
 */
public class DeleteBatchByIds extends Method {

    public DeleteBatchByIds() {
        this(SqlMethod.DELETE_BATCH_BY_IDS.getMethod());
    }

    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public DeleteBatchByIds(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BATCH_BY_IDS;
        {
            sqlMethod = SqlMethod.DELETE_BATCH_BY_IDS;
            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
                    SqlScriptUtils.convertForeach(
                            SqlScriptUtils.convertChoose(
                                    "@org.apache.ibatis.type.SimpleTypeRegistry@isSimpleType(item.getClass())",
                                    "#{item}", "#{item." + tableInfo.getKeyProperty() + "}"),
                            COLL, null, "item", COMMA));
            SqlSource sqlSource = super.createSqlSource(configuration, sql, Object.class);
            return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
        }
    }

}
